//
//  FindPasswordViewController.swift
//  maosheng
//
//  Created by 何海涛 on 2019/8/13.
//

import Foundation

class FindPasswordViewController: RootViewController, LoginInputViewDelegate, FindPwdViewDelegate {
    
    var findPwdView: FindPwdView!
    var isRegist : Bool = false
    override func viewDidLoad() {
        super.viewDidLoad()
        self.edgesForExtendedLayout = UIRectEdge(rawValue: 0)
        
        self.view.backgroundColor = UIColor.white
        self.title = isRegist ? "注册" : "找回登录密码"

        findPwdView = FindPwdView()
        findPwdView.isRegist = isRegist
        self.view.addSubview(findPwdView)
        findPwdView.snp.makeConstraints { (make) in
            make.edges.equalTo(self.view)
        }
        
        findPwdView.render()
        findPwdView.delegate = self
        findPwdView.inputCode.delegate = self
    }
    
    func sendValidateCode() {
        // todo: send validation code.
//        let phoneNum = findPwdView.inputMobile.txt.text
//        SDataCenter.shared.sendVertifyCode(type: isRegist ? 1 : 3, phoneNum: phoneNum) { (isSuccess) in
//            <#code#>
//        }
//        ProgressHUD.show(<#T##msg: String!##String!#>, in: <#T##UIView!#>, mode: <#T##ProgressMode#>)
        let phoneNum = findPwdView!.inputMobile.txt.text!
        let isValid = findPwdView.inputCode.checkPhoneValid(phoneNum: phoneNum)
        if isValid{
            SDataCenter.shared.sendVertifyCode(type: isRegist ? 1 : 3, phoneNum: phoneNum) { [weak self](isSuccess) in
                if isSuccess{
                    self?.findPwdView.inputCode.beginCoundown()
                }
            }
        }
    }
//    “verifyType”: 1,
//    “verifyValue”: “string”,
//    “securityCode”: “string”,
//    “password”: “string”,
//    “confirmPassword”: “string”
    
//    "phoneNumber": "string",
//    "securityCode": "string",
//    "confirmPassword": "string",
//    "password": "string"
    func sendResetPwd(_ model: FindPasswordLoginRequest) {
        // todo: send request.
        let str_url = isRegist ? "api/services/app/Account/RegisterMobileAsync" : ""
        var params = [String : Any]()
        if isRegist{
            params["phoneNumber"] = findPwdView!.inputMobile.txt.text!
            params["securityCode"] = findPwdView.inputCode.txt.text
            params["password"]  = findPwdView.inputPwd.txt.text
        }else{
            params["verifyValue"] = findPwdView!.inputMobile.txt.text!
            params["securityCode"] = findPwdView.inputCode.txt.text
            params["password"]  = findPwdView.inputPwd.txt.text
            params["verifyType"]  = 1
        }
        params["confirmPassword"]  = findPwdView.inputPwd.txt.text

        SDataCenter.shared.SPostJsonDataWithUrl(str_url: str_url, params: params) { (responseData, isSuccess) in
            if isSuccess{
                ProgressHUD.showMsgWithoutView(self.isRegist ? "注册成功" : "密码重置成功")
                DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1.5, execute: {
                    self.navigationController?.popViewController(animated: true)
                })
            }
        }
    }
}
